<template>
  <div class="base-container" id="main">
    <t-space direction="vertical">
      <t-form labelAlign="top" ref="form" :data="formData" :rules="rules" class="step-form table-form"
              labelWidth="140px">
        <t-row>
          <t-col :span="2" class="title"><span class="require">*</span>年度</t-col>
          <t-col :span="2">
            <t-form-item label="年度" name="year">
              <t-select clearable v-model="formData.year" :options="yearOption" />
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
      <t-row>
        <t-col :span="1"></t-col>
        <t-col :span="10">
          <t-table
            rowKey="index"
            :data="data"
            :columns="columns"
            stripe
            table-layout="auto"
            size="small"
            hover
            tableLayout="fixed"
            bordered
            cellEmptyContent="-"
          >
            <template #name="{row}">
              <p :class="row.class">{{ row.name }}</p>
            </template>
            <template #num="{row}">
              <!-- :disabled="preData.inputDisabled.includes(row.num)" -->
              <t-input-number v-if="row.num !== ''" min="0" size="large" v-model="formData[`${row.num}`]"></t-input-number>
            </template>
            <template #footerSummary>
              <div class="t-table__row-filter-inner">
                说明：1.主要平衡关系：KM040= KM0401+ KM0402
                <br/>      2.科普传媒是指各填报单位产出的科普作品，而不是填报单位订阅的资料。
                <br/>      3.科普图书需要取得ISBN编号，科普期刊和科技类报纸需要取得国内统一连续出版物号，科普电影需要取得电影片公映许可证。
                <br/>      4.KM500和KM600由广播电视部门和宣传部门填报。
              </div>
            </template>
          </t-table>
        </t-col>
        <t-col :span="1"></t-col>
      </t-row>
      <t-row style="margin-top: 20px;text-align: center">
        <t-col :span="12">
          <t-space :size="10">
            <t-button v-if="!isUpdate" theme="primary" :loading="applyLoading" @click="onSubmit(1)">{{
                applyLoading ? '提交中...' : '暂存'
              }}
            </t-button>
            <t-button v-if="!isUpdate" theme="primary" :loading="applyLoading" @click="onSubmit(2)">{{
                applyLoading ? '提交中...' : '提交'
              }}
            </t-button>
            <t-button v-if="isUpdate" theme="default" :loading="applyLoading" @click="onSubmit(3)">{{
                '取消'
              }}
            </t-button>
            <t-button v-if="isUpdate" theme="primary" :loading="applyLoading" @click="onSubmit(4)">{{
                applyLoading ? '修改中...' : '确认'
              }}
            </t-button>
          </t-space>
        </t-col>
      </t-row>
    </t-space>
  </div>
</template>

<script>
import { VITE_FILE_DOMAIN } from '@/api/upload'
export default {
  name: "media-add-edit",
  dicts: [],
  components: {
  },
  data() {
    return {
      yearOption: [],
      file_domain: VITE_FILE_DOMAIN,
      deptMenuOptions: [],
      columns: [
        // { colKey: 'index', title: '序号', width: '100' },
        { colKey: 'name', title: '指标名称', },
        { colKey: 'code', title: '代码', align: "center" },
        { colKey: 'jldw', title: '计量单位', align: "center"},
        { colKey: 'num', title: '数量', align: "center"},
      ],
      rules: {
        projectName: [
          {
            required: true,
            message: "项目名称不能为空",
          }
        ],
      },
      data: [],
      preData: {
        name: [
          '一、科普图书',
          '  1.当年出版种数',
          '  2.当年出版总册数',
          '二、科普期刊',
          '  1.当年出版种数',
          '  2.当年出版总册数',
          '三、科技类报纸当年发行总份数',
          '四、科普电影',
          '  1. 当年放映片源数量',
          '    其中：国产数量',
          '    进口数量',
          '  2. 当年观众数量',
          '五、电视台当年播出科普（技）节目时长',
          '六、电台当年播出科普（技）节目时长',
          '七、科普网站',
          '  1.建设数量',
          '  2.当年访问数量',
          '  3.当年发文数量',
          '八、当年发放科普读物和资料',
          '九、科普类微博',
          '  1.建设数量',
          '  2.当年发文数量',
          '  3.当年阅读数量',
          '  4.粉丝数量',
          '十、科普类微信公众号',
          '  1.建设数量',
          '  2.当年发文数量',
          '  3.当年阅读数量',
          '  4.关注数量',
          '十一、网络科普视频',
          '  1.当年发布数量',
          '  2.当年发布时长',
          '  3.当年播放数量',
        ],
        code:[
          '—',
          '种',
          '册',
          '—',
          '种',
          '册',
          '份',
          '-',
          '部',
          '部',
          '部',
          '人次',
          '小时',
          '小时',
          '-',
          '个',
          '次',
          '篇',
          '份',
          '-',
          '个',
          '篇',
          '次',
          '个',
          '-',
          '个',
          '篇',
          '次',
          '个',
          '-',
          '个',
          '小时',
          '次',
        ],
        jldw: [
          '—',
          'KM110',
          'KM120',
          '—',
          'KM210',
          'KM220',
          'KM400',
          '-',
          'KM040',
          'KM0401',
          'KM0402',
          'KM041',
          'KM500',
          'KM600',
          '-',
          'KM700',
          'KM710',
          'KM720',
          'KM800',
          '-',
          'KM010',
          'KM011',
          'KM012',
          'KM013',
          '-',
          'KM020',
          'KM021',
          'KM022',
          'KM023',
          '-',
          'KM030',
          'KM031',
          'KM032',
        ],
        num: [
          '',
          'kptscbzs',
          'kptscbzcs',
          '',
          'kpqkcbzs',
          'kpqkcbzcs',
          'kjlbzfxzs',
          '',
          'kpdyfyzs',
          'kpdygczs',
          'kpdyjkzs',
          'kpdygzzs',
          'dstkpjmzsc',
          'dtkpjmzsc',
          '',
          'kpwzjssl',
          'kpwzfwsl',
          'kpwzfwwsl',
          'kpdwzlzs',
          '',
          'kpwbjszs',
          'kpwbfwzs',
          'kpwbydzs',
          'kpwbfszs',
          '',
          'kpgzhjszs',
          'kpgzhfwzs',
          'kpgzhydzs',
          'kpgzhgzzs',
          '',
          'kpspfbzs',
          'kpspfbsczs',
          'kpspbfzs',
        ],
        class: [
          '',
          'indent2',
          'indent2',
          '',
          'indent2',
          'indent2',
          '',
          '',
          'indent2',
          'indent4',
          'indent4',
          'indent2',
          '',
          '',
          '',
          'indent2',
          'indent2',
          'indent2',
          '',
          '',
          'indent2',
          'indent2',
          'indent2',
          'indent2',
          '',
          'indent2',
          'indent2',
          'indent2',
          'indent2',
          '',
          'indent2',
          'indent2',
          'indent2',
        ],
        inputDisabled: []
      },
      applyLoading: false,
      formData: {
        localId: '',
        year: new Date().getFullYear(),
        kpjfcj: 0,
        zfbkcj: 0,
        kpzxjfcj: 0,
        jzcj: 0,
        zccj: 0,
        kpjfsy: 0,
        xzzcsy: 0,
        kphdzcsy: 0,
        kjhdzcsy: 0,
        kpcgzcsy: 0,
        zfbkzcsy: 0,
        kpzpzcsy: 0,
        qtzcsy: 0,
      },
      isUpdate: false,
    }
  },
  created() {
    for (let i = 2030; i >= 2000; i--) {
      this.yearOption.push({key: i, label: i.toString(), value: i.toString()})
    }
    const taskId = this.$route.params && this.$route.params.taskId;
    this.isUpdate = this.$route.query.isUpdate === 'true'
    Object.assign(this.formData, this.$route.query.currentRow)
    if (!taskId) {
      this.$message.error("缺少id");
      this.close();
      return false;
    }
    this.formData.localId = taskId

    for (let i = 0; i < this.preData.name.length; i++) {
      this.data.push({
        index: i + 1,
        name: this.preData.name[i],
        code: this.preData.code[i],
        jldw: this.preData.jldw[i],
        num: this.preData.num[i],
        class: this.preData.class[i]
      })
    }
  },
  methods: {
    onSubmit(type) {
      if (!this.formData.year) {
        this.$message.error('请选择年份！');
        return
      }
      if (type === 1) {
        this.$api.kpzh.media.add(this.formData).then(res => {
          this.$message.success('暂存成功！');
          this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/media/media-my"});
        })
      } else if (type === 2) {
        this.$api.kpzh.media.submit(this.formData).then(res => {
          this.$message.success('提交成功！');
          this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/media/media-my"});
        })
      } else if (type === 3) {
        this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/media/media-my"});
      } else if (type === 4) {
        this.$api.kpzh.media.update(this.formData).then(res => {
          this.$message.success('修改成功！');
          this.$tab.closeOpenPage({path: this.$route.meta.activeMenu || "/kpzh/media/media-my"});
        })
      } else {
        this.$message.error('type错误！');
      }
    },
    close() {
      this.$tab.closeOpenPage({path: "/kpzh/media/media-my"});
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .t-table__empty {
  min-height: initial;
}

</style>
